
// 小时 天 月切换
function change() {
    var hdm = $("#hdm").val();
    if(hdm == 1){
        $("#test6").hide();
        $("#test8").hide();
        $("#test10").show();
    }else if(hdm == 2){
        $("#test6").show();
        $("#test8").hide();
        $("#test10").hide();
    }else if(hdm == 3){
        $("#test6").hide();
        $("#test8").show();
        $("#test10").hide();
    }
}

function category() {
    var id = $("#category").val();
    var text = '';
    if (id==0){
        text='<option value='+0+'>请选择具体服务类别</option>'
        $("#categoryDetail").html(text)
    }else {
        $.ajax({
            //提交数据的类型 POST GET
            type: "GET",
            //提交的网址
            url: "/services/getCategoryDetail",
            //提交的数据
            data: {id: id},
            dataType: 'json',
            //参数格式为json
            // contentType: "application/json; charset=utf-8",
            //返回数据的格式
            // datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
            //成功返回之后调用的函数
            success: function (res) {
                // console.log(data.length);
                var len = res.length;
                for (var i = 0; i < len; i++) {
                    // data.Foreach(function(data){
                    var data = res[i];
                    text += '<option value=' + data.id + '>' + data.name + '</option>';
                }
                $("#categoryDetail").html(text)

            },
            //调用出错执行的函数
            error: function () {
                //请求出错处理
                alert("请求失败");
            }

        })
    }
}
$('#myfile').change( function() {
    $("#unpicture").hide();
    // $("#pictureId").show();
    $("#path").ajaxSubmit({
        url: "/test/upload",
        method: 'POST',
        datType: 'json',

        success: function (data) {
            console.log(data.imgUrl);
            console.log(typeof data.imgUrl);
            var ele = "<img src=\"\" id='pictureId' class='pictureId' style='height: 160px; width: 160px;border-radius: 10px;'/>";
            $("#preview").append(ele);//编程一个jquery的对象
            $("#preview").find("img").attr("src",data.imgUrl);//调用jquery的方法找到
//                        图片的标签的名称，再设置里面的src的属性的值

        },
        error: function (data) {
            alert(data);
        }
    })
});

$(document).ready(function () {
    dropdownOpen();//调用
});
/**
 * 鼠标划过就展开子菜单，免得需要点击才能展开
 */
function dropdownOpen() {

    var $dropdownLi = $('li.dropdown');

    $dropdownLi.mouseover(function () {
        $(this).addClass('open');
    }).mouseout(function () {
        $(this).removeClass('open');
    });
}

layui.use('laydate', function(){
    var laydate = layui.laydate;
    //控制发布服务的时间,当前时间
    var myDate = new Date();
    var minTime = myDate.toLocaleString();
    //年月范围
    laydate.render({
        elem: '#test8'
        ,type: 'month'
        ,range: '~'
        ,min: minTime
    });
    //日期范围
    laydate.render({
        elem: '#test6'
        ,range: '~'
        ,min: minTime
    });

    //日期时间范围
    laydate.render({
        elem: '#test10'
        ,type: 'datetime'
        ,range: '~'
        ,format: 'yyyy-MM-dd HH'
        ,min: minTime
    });
});

$(document).ready(function () {
    $('.save').click(function () {
        swal({
            title: "保存到草稿成功",
            text: "返回继续编辑发布服务",
            type: "success"
        });
    });
    $('.commit').click(function () {
        $.ajax({
            //提交数据的类型 POST GET
            type:"POST",
            //提交的网址
            url:"/services/serviceRelease",
            //提交的数据
            data: JSON.stringify(GetJsonData()),
            //参数格式为json
            contentType: "application/json; charset=utf-8",
            //返回数据的格式
            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
            //成功返回之后调用的函数
            success:function(data){
                console.log(data);
                if("OK" == data.codeStatus){

                }else {
                    alert("失败")
                }
            },
            //调用出错执行的函数
            error: function(){
                //请求出错处理
                console.log(JSON.stringify(GetJsonData()));
                alert("请求失败");
            }

        });
        swal({
            title: "您成功的发布了一个服务",
            type: "success",
            confirmButtonText: "OK",

        }, function () {
            window.location="/services/serviceListView" //服务列表界面
        });

    });
//json格式的数据源
    function GetJsonData() {
        var userinfoId = $("#userinfoId").text();
        var category = $("#category").val();
        var categoryDetail = $("#categoryDetail").val();
        var title = $("#title").val();
        var path = $('#pictureId').attr('src');
        var phone = $("#cellPhone").val();
        var province = $("#province1").find("option:selected").text();
        var city = $("#city1").find("option:selected").text();
        var area = $("#district1").find("option:selected").text();
        var price = $("#price").val();
        var intro = $("#intro").val();

        var hdm = $("#hdm").val();
        if(hdm == 1){
            var time = $("#test10").val();
            var chargetype = "小时";
            var times = time.split(" ~ ");
            var beginTime = times[0]+ ':00:00';
            var endTime = times[1]+ ':00:00';
        }else if(hdm == 2){
            var time = $("#test6").val();
            var chargetype = "天";
            var times = time.split(" ~ ");
            var beginTime = times[0]+ ' 00:00:00';
            var endTime = times[1]+ ' 00:00:00';
        }else if(hdm == 3){
            var time = $("#test8").val();
            var chargetype = "月";
            var times = time.split(" ~ ");
            var beginTime = times[0]+ '-01 00:00:00';
            var endTime = times[1]+ '-01 00:00:00';
        }




        var json = {
            "userinfoId": userinfoId,
            "categoryId": category,
            "categorydetailId": categoryDetail,
            "title": title,
            "path": path,
            "phone": phone,
            "province": province,
            "city": city,
            "area": area,
            "price": price,
            "chargetype": chargetype,
            "count": "0",
            "intro": intro,
            "status": "0",
            "beginTime": beginTime,
            "endTime": endTime
        };
        return json;
    }

});

// 判断手机号
function checkTel(){
    var registerPhone = document.getElementById("registerPhone").value;
    var phoneRegex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    document.getElementById("registerPassword").placeholder='请输入您的手机号'

    if(registerPhone.length ==0 ){
        document.getElementById("error-tip1").innerText = "手机号不能为空";

    }else{
        if(!(phoneRegex.test(registerPhone))){
            document.getElementById("error-tip1").innerText = "手机号格式错误";
        }
    }
}
//控制标题输入字数
function controlLen() {
    //获取input输入框元素
    var inputText = document.getElementById('title').value;
    if (inputText.length > 30) {
        var text = inputText.substring(0, 30);
        document.getElementById('title').value = text;//从新设置input输入框的值
        alert("最多输入30个字符");
    }
}
//控制介绍输入的字数
function controllen() {
    //获取input输入框元素
    var inputText = document.getElementById('intro').value;
    if (inputText.length > 1000) {
        var text = inputText.substring(0, 1000);
        document.getElementById('intro').value = text;//从新设置input输入框的值
        alert("最多输入1000个字符");
    }
}
// $(function(){
//     //隐藏警告信息
//     $(".alert-warning").hide();
//     $("#commit").click(function(){
//     });
// });